package zhixin;

import lombok.var;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.v106.network.Network;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class EchartsAuto {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "driver/chromedriver108.0.5359.22.exe");
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("debuggerAddress", "localhost:9222");
        ChromeDriver driver = new ChromeDriver(options);
        // 初始化监听
        var devTools = driver.getDevTools();
        devTools.createSession();
        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
        var exampleListPanel = driver
                .findElement(By.xpath("/html/body/div[3]/div/div[1]/div/div[2]/div")) // exampleListPanel
                .findElements(By.cssSelector("div>div>div>a"))
                .stream()
                .map(i -> i.getAttribute("href"))
                .collect(Collectors.toList()); // a
        System.out.println(exampleListPanel.size());
        AtomicInteger firstPageIndex = new AtomicInteger(12);
        AtomicInteger count = new AtomicInteger();
        var href = exampleListPanel.get(firstPageIndex.get());
        System.out.println(href + "----打开");
        driver.get(href);
        devTools.addListener(Network.requestIntercepted(), requestIntercepted -> {
            count.getAndIncrement();
        });
        devTools.addListener(Network.responseReceived(), responseReceived -> {
            int i = count.decrementAndGet();
            if (i == 0) {
                int andIncrement = firstPageIndex.getAndIncrement();
                if (andIncrement < exampleListPanel.size()) {
                    var s = exampleListPanel.get(andIncrement);
                    System.out.println(s + "----打开");
                    driver.get(s);
                }
            }
        });
    }
}
